package com.api.ld.web;

import com.engine.ld.functions.sap.cgsq.ZMM_PURCHASEREQUEST_IMPProxy;
import com.engine.ld.optimize.PurchasingRequisitionParseXmlUtil;
import com.engine.ld.utils.DealXml;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

/**
 * @author ZhangLei
 * @date 2021/7/1
 */
public class CgsqAction implements Action {
    BaseBean bb = new BaseBean();

    @Override
    public String execute(RequestInfo requestInfo) {
        bb.writeLog("step0------------");

        return CgsqHandle(requestInfo.getRequestid());
    }

    private String CgsqHandle(String requestid) {

        String back = "";
        try {
            bb.writeLog("step1------------");
            ZMM_PURCHASEREQUEST_IMPProxy pp = new ZMM_PURCHASEREQUEST_IMPProxy();
            bb.writeLog("step2------------");
            String SAPBackXml = pp.getZMM_PURCHASEREQUEST_IMP_PortType().ZMM_PURCHASEREQUEST_IMP(new PurchasingRequisitionParseXmlUtil().parseImportXml(requestid));
//            String SAPBackXml = pp.getZMM_PURCHASEREQUEST_IMP_PortType().ZMM_PURCHASEREQUEST_IMP(normXML);
//            bb.writeLog("传给SAP的参数是：" + normXML);
            bb.writeLog("SAP返回的结果是：" + SAPBackXml);
            Map<String, Object> sapBackMap = new DealXml().XmlResultMap(SAPBackXml);
            bb.writeLog("step3------------");
            String type = sapBackMap.get("TYPE").toString();
            String message = sapBackMap.get("MESSAGE").toString();
            message = message.contains("'") ? message.replace("'", "''") : message;
            bb.writeLog("解析后，type:" + type + ";message:" + message);

            boolean b = saptodatabase(type, message, requestid);

            bb.writeLog("SAP返回结果添加到数据库：" + b);


            if ("S".equals(type)) {
//                如果SAP的返回结果是成功则将生成的订单号添加到数据库
                bb.writeLog("准备进行订单号添加到数据库");
                String zflag = "";
                String[] split = message.split(":");
                bb.writeLog("拆分后的message信息为："+split);
                String getZFLAG = "select ZFLAG from formtable_main_120 a left join formtable_main_120_dt1 dt on a.id = dt.mainid where requestId = " + requestid;
                bb.writeLog("获取业务标识的sql为："+getZFLAG);
                RecordSet recordSet = new RecordSet();
                boolean b1 = recordSet.executeQuery(getZFLAG);
                bb.writeLog("查询业务标识的结果: "+b1);
                if (recordSet.next()) {
//                    recordSet.getString("ZFLAG");
                    zflag = Util.null2String(recordSet.getString("ZFLAG"));
                    bb.writeLog("获取到的业务标识为："+zflag);
                }else {
                    bb.writeLog("从数据库中获取业务标识错误");
                }
                if ("1".equals(zflag)){
                    String banfn = split[1];
                    bb.writeLog("获取的申请编号是："+banfn);
                    String updateSql = "update formtable_main_120_dt1 set BANFN = "+banfn+" where mainid = (select id from formtable_main_120 where requestId = "+requestid+")";
                    bb.writeLog("申请编号的赋值语句："+updateSql);
                    RecordSet recordSet1 = new RecordSet();
                    boolean b2 = recordSet1.executeUpdate(updateSql);
                    bb.writeLog("向数据库中更新采购编号的结果"+b2);
                }
                back = Action.SUCCESS;
            } else {
                back = Action.FAILURE_AND_CONTINUE;
                bb.writeLog("SAP返回结果为E,MESSAGE为：" + message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return back;
    }


    /*将sap返回的结果存入数据库*/
    private boolean saptodatabase(String type, String message, String requestid) {
        String sql = "UPDATE formtable_main_120 SET type='" + type + "',message='" + message + "' WHERE requestId=" + requestid;
        RecordSet rs = new RecordSet();
        return rs.executeUpdate(sql);
    }
}
